package com.whw.fy.service.impl;

import com.whw.fy.entity.*;
import com.whw.fy.mapper.*;
import com.whw.fy.utils.AgeUtil;
import com.whw.fy.utils.GetEveryDate;
import com.whw.fy.utils.ListUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.whw.fy.service.PlansclazzService;
@Service
public class PlansclazzServiceImpl implements PlansclazzService{

    @Resource
    private StudentMapper studentMapper;
    @Resource
    private PlansclazzMapper plansclazzMapper;
    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private LessonMapper lessonMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private RoomMapper roomMapper;

    @Override
    public Message deleteByPrimaryKey(Integer plansclazzId) {
        return plansclazzMapper.deleteByPrimaryKey(plansclazzId)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message insertSelective(Plansclazz record) {
        return plansclazzMapper.insertSelective(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer plansclazzId) {
        Plansclazz plansclazz = plansclazzMapper.selectByPrimaryKey(plansclazzId);
        return Message.success(null).add(initPlansClazz(plansclazz));
    }

    @Override
    public Message updateByPrimaryKeySelective(Plansclazz record) {
        return plansclazzMapper.updateByPrimaryKeySelective(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message selectALL() {
        List<Plansclazz> plansclazzList = plansclazzMapper.selectALL();
        for (Plansclazz plansclazz:plansclazzList){
            plansclazz = initPlansClazz(plansclazz);
        }
        return Message.success(null).add(plansclazzList);
    }

    @Override
    public Message selectallByTeacherIdJurisdictionEight(int staffId) {
        List<Clazz> clazzList = clazzMapper.selectByClassTeacherId(staffId);
        List<Integer> clazzIdList = new LinkedList<>();
        for (Clazz clazz:clazzList){
            clazzIdList.add(clazz.getClassId());
        }
        clazzIdList = ListUtil.duplicate(clazzIdList);
        List<Plansclazz> plansclazzList = new ArrayList<>();

        for (Integer id:clazzIdList){
            List<Plansclazz> plansclazzList1 = plansclazzMapper.selectAllByPlansclazzClazzId(id);
            plansclazzList.addAll(plansclazzList1);
        }
        for (Plansclazz plansclazz:plansclazzList){
            plansclazz = initPlansClazz(plansclazz);
        }
        return Message.success(null).add(plansclazzList);
    }

    @Override
    public Message selectallByMedicalDirectorIdJurisdictionZero(int staffId) {
        List<Clazz> clazzList = clazzMapper.selectByClassMedicalDirectorId(staffId);
        List<Integer> clazzIdList = new LinkedList<>();
        for (Clazz clazz:clazzList){
            clazzIdList.add(clazz.getClassId());
        }
        clazzIdList = ListUtil.duplicate(clazzIdList);
        List<Plansclazz> plansclazzList = new ArrayList<>();

        for (Integer id:clazzIdList){
            List<Plansclazz> plansclazzList1 = plansclazzMapper.selectAllByPlansclazzClazzId(id);
            plansclazzList.addAll(plansclazzList1);
        }
        for (Plansclazz plansclazz:plansclazzList){
            plansclazz = initPlansClazz(plansclazz);
        }
        return Message.success(null).add(plansclazzList);
    }

    @Override
    public Message selectallByTeachingDirectorJurisdictionSix(int staffId) {
        List<Plansclazz> plansclazzList = plansclazzMapper.selectByPlansclazzTeachingSupervisor(staffId);
        for (Plansclazz plansclazz:plansclazzList){
            plansclazz = initPlansClazz(plansclazz);
        }
        return Message.success(null).add(plansclazzList);
    }

    @Override
    public Message selectallByprincipalTeacherJurisdictionSeven(int staffId) {
        List<Clazz> clazzList = clazzMapper.selectByClassResponsibleTeacherId(staffId);
        List<Integer> clazzIdList = new LinkedList<>();
        for (Clazz clazz:clazzList){
            clazzIdList.add(clazz.getClassId());
        }
        clazzIdList = ListUtil.duplicate(clazzIdList);
        List<Plansclazz> plansclazzList = new ArrayList<>();

        for (Integer id:clazzIdList){
            List<Plansclazz> plansclazzList1 = plansclazzMapper.selectAllByPlansclazzClazzId(id);
            plansclazzList.addAll(plansclazzList1);
        }
        for (Plansclazz plansclazz:plansclazzList){
            plansclazz = initPlansClazz(plansclazz);
        }
        return Message.success(null).add(plansclazzList);
    }



    @Override
    public Message selectAllByPlansclazzClazzId(Integer plansclazzClazzId) {
        List<Plansclazz> plansclazzList = plansclazzMapper.selectAllByPlansclazzClazzId(plansclazzClazzId);
        for (Plansclazz plansclazz:plansclazzList){
            plansclazz = initPlansClazz(plansclazz);
        }
        return Message.success(null).add(plansclazzList);
    }

    @Override
    public Message updateBatch(List<Plansclazz> list) {
        return plansclazzMapper.updateBatch(list)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message updateBatchSelective(List<Plansclazz> list) {
        return plansclazzMapper.updateBatchSelective(list)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message batchInsert(List<Plansclazz> list) {
        return plansclazzMapper.batchInsert(list)>0?Message.success(null):Message.fail(null);
    }



    private Plansclazz initPlansClazz(Plansclazz plansclazz){
        //设置学生姓名
        if (plansclazz.getPlansclazzStudentId() != null) {
            plansclazz.setPlansclazzStudentName(studentMapper.selectByPrimaryKey(plansclazz.getPlansclazzStudentId()).getStudentName());
        }
        //设置坑成类型和技术类别
        if (plansclazz.getPlansclazzClazzId()!=null){
            Clazz clazz = clazzMapper.selectByPrimaryKey(plansclazz.getPlansclazzClazzId());
            Lesson lesson = lessonMapper.selectByPrimaryKey(clazz.getClassCourseId());
            plansclazz.setTechnicalTypeName(lesson.getTechnicalType().getTechnicalTypeName());
            plansclazz.setClazzTypeName(clazz.getClazz_type().getClazzTypeName());
        }
        //设置授课老师名称
        if (plansclazz.getPlansclazzPeopleTeacherId()!=null)
        plansclazz.setPlansclazzPeopleTeacherName(staffMapper.selectByPrimaryKey(plansclazz.getPlansclazzPeopleTeacherId()).getStaffName());
        //设置教学主管名称
        if (plansclazz.getPlansclazzTeachingSupervisor()!=null)
        plansclazz.setPlansclazzTeachingSupervisorName(staffMapper.selectByPrimaryKey(plansclazz.getPlansclazzTeachingSupervisor()).getStaffName());
        //设置医疗主管名称
        if (plansclazz.getPlansclazzMedicalDirector()!=null)
        plansclazz.setPlansclazzMedicalDirectorName(staffMapper.selectByPrimaryKey(plansclazz.getPlansclazzMedicalDirector()).getStaffName());

        //设置地点名称
        if (plansclazz.getPlansclazzRoomId()!=null)
            plansclazz.setPlansclazzRoomName(roomMapper.selectByPrimaryKey(plansclazz.getPlansclazzRoomId()).getRoomLoc());
        return plansclazz;
    }
}
